<?php

namespace backend\modules\personnel\controllers;

use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\personnel\Dombs;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class DombsController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Dombs::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $id = Yii::$app->request->get();
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['position'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andwhere(['=','group',$id['id']])
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'hhh'=>$id['id'],
        ]);
    }

    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $group =Yii::$app->request->get('group');
        $model = $this->findModel($id);
        if($group){
            $model->group =$group;
        }
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }
        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'menuDropDownList' => Yii::$app->services->menu->getDropDownts(),
        ]);
    }

    public function actionUpdate()
    {
        $id=Yii::$app->request->get();
        $data=Dombs::findOne($id);
        $data->leader=1;
        $arr =Dombs::find()
            ->where(['group'=>$data['group']])
            ->asArray()
            ->all();
       $a= array_column($arr,'id');
       $arr =Dombs::updateAll(['leader'=>0],['in','id',$a]);
        if($data->save()){
            return $this->message("设置成功", $this->redirect(Yii::$app->request->referrer));
        }
    }
}